﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using nwrole.plugin;

namespace nwrole.lib2 {
    public class PluginWrapper {
        public IPlugin Plugin { get; private set; }
        public Thread Thread { get; private set; }
        public TimeSpan StartDelay { get; set; }

        public PluginWrapper(IPlugin plugin) {
            StartDelay = new TimeSpan(0);
            Plugin = plugin;
        }

        public void Start() {
            Thread = new Thread(ThreadStart);
            Thread.Start();
        }

        private void ThreadStart() {
            System.Threading.Thread.Sleep(StartDelay);
            Plugin.Start();
        }

        public void Stop() {
            if (Thread == null || !Thread.IsAlive)
                return;

            Plugin.Stop();
            Thread.Join(TimeSpan.FromSeconds(10));
            Thread = null;
        }
    }
}
